package cn.rui.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.hutool.core.util.IdUtil;
import cn.rui.common.ContentBean;
import cn.rui.common.ResponseBean;
import cn.rui.pojo.goods.Goods;
import cn.rui.pojo.vo.PageVo;
import cn.rui.service.goods.GoodsService;
import cn.rui.utils.QiniuUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author 徽州大都督
 * @date 2021/6/21
 */
@RestController
@RequestMapping("goods")
@Api(tags = "商品模块")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @ApiOperation(value = "改变商品状态")
    @SaCheckLogin
    @PutMapping("updateEnable/{id}")
    public ResponseBean updateEnable(@PathVariable("id") Long id) {
        return goodsService.updateEnable (id);
    }


    @ApiOperation(value = "分页查询商品")
    @SaCheckLogin
    @PostMapping("findPage")
    public ResponseBean findPage(@RequestBody PageVo pageVo, HttpSession session) {
        return goodsService.findPage (pageVo);
    }

    @ApiOperation(value = "修改图片并保存商品信息")
    @SaCheckLogin
    @PostMapping("updateGoods")
    public ResponseBean updateGoods(@RequestBody Goods goods) {
        return goodsService.updateGoods (goods);
    }

    @ApiOperation(value = "上传商品图片")
    @PostMapping("upload")
    public ResponseBean upload(MultipartFile file) {


        //生成的是不带-的字符串，类似于：b17f24ff026d40949c85a24f4f375d42
        String uuid = IdUtil.simpleUUID ();
        //String oldName = file.getOriginalFilename ();  //文件原始文件名
        String oldName = file.getOriginalFilename ();
        int index = oldName.lastIndexOf (".");//获得文件后缀所在的下标
        String ext = oldName.substring (index - 1);  // .jpg
        String imgName = uuid + ext;

        //使用封装的七牛云工具类上传到云服务器
        try {
            QiniuUtils.upload2Qiniu (file.getBytes (), imgName);
            return ResponseBean.success (ContentBean.UploadSuccess, imgName);
        } catch (IOException e) {
            e.printStackTrace ();
            return ResponseBean.error (ContentBean.UploadError);
        }

    }

    @ApiOperation(value = "修改商品图片")
    @PostMapping("upload2/{id}")
    public ResponseBean upload2(MultipartFile file,@PathVariable("id")Long id) {

        //生成的是不带-的字符串，类似于：b17f24ff026d40949c85a24f4f375d42
        String uuid = IdUtil.simpleUUID ();
        //String oldName = file.getOriginalFilename ();  //文件原始文件名
        String oldName = file.getOriginalFilename ();
        int index = oldName.lastIndexOf (".");//获得文件后缀所在的下标
        String ext = oldName.substring (index - 1);  // .jpg
        String imgName = uuid + ext;

        //使用封装的七牛云工具类上传到云服务器
        try {
            QiniuUtils.upload2Qiniu (file.getBytes (), imgName);
            goodsService.updateImage(id,imgName);
            return ResponseBean.success (ContentBean.UploadSuccess, imgName);
        } catch (IOException e) {
            e.printStackTrace ();
            return ResponseBean.error (ContentBean.UploadError);
        }

    }

    @ApiOperation(value = "添加商品信息")
    @PostMapping("save")
    @SaCheckLogin
    public ResponseBean upload(@RequestBody Goods goods) {
        return goodsService.save(goods);
    }

    @ApiOperation(value = "删除商品信息")
    @DeleteMapping("deleteById/{id}")
    @SaCheckLogin
    public ResponseBean deleteById(@PathVariable("id")Long id) {
        return goodsService.deleteById(id);
    }
}
